package com.study.mybatisplus.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.mybatisplus.entity.User;
import com.study.mybatisplus.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author flwu
 * @since 2020-03-20
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/info")
    public User userInfo() {
        try {
            return userService.info(2);
        } catch (Exception e) {
            return null;
        }
    }

    @RequestMapping(value = "/list")
    public List<User> userList() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.last("limit 20");
        wrapper.orderByDesc("id");
        List<User> userList = userService.list(wrapper);
        return userList;
    }

    @RequestMapping(value = "/list/{page}/{pageSize}")
    public IPage<Map<String, Object>> userListByPage(@PathVariable(value = "page") Integer page, @PathVariable(value= "pageSize") Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("phone", "nickname");
        Page userPage = new Page(page, pageSize);
        //如果使用Ipage<User>来接受结果，打印出来的没有指定的查询字段全是null
        //IPage<User> userList = userService.page(userPage, wrapper);
        //使用Ipage<map>来接受，只会输出查询的字段，比较符合业务场景
        IPage<Map<String, Object>> userList1 = userService.pageMaps(userPage, wrapper);
        return userList1;
    }

    @RequestMapping(value = "/update")
    public Boolean update() {
        String newName = "亮爷";
        User user = new User();
        user.setNickname(newName);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", 2);
        boolean result = false;
        try {
            result = userService.udpate(user, 2);
        } catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }

    @RequestMapping(value = "/add")
    public Integer add() {
        User user = new User();
        user.setNickname("lil222si");
        user.setPhone("18521702922");
        Integer userId = 0;
        try {
            userId = userService.add(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return userId;
    }

    @RequestMapping(value = "/find")
    public User findByPhone() {
        User user = null;
        try {
           user =  userService.findUserByPhone("18521702935");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    @RequestMapping(value = "/find-id")
    public Map<String, Object> findById() {
        Map<String, Object> userBase = null;
        try {
            userBase =  userService.findUserById(2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userBase;
    }

}
